package cn.itcast.nems.order.service.impl;

import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.crypto.digest.MD5;
import cn.itcast.nems.common.entity.BaseEntity;
import cn.itcast.nems.order.dao.entity.OrderCustomerRelationship;
import cn.itcast.nems.order.dao.mapper.OrderCustomerRelationshipMapper;
import cn.itcast.nems.order.service.OrderCustomerRelationshipService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 销售订单-客户关系 服务实现类
 * </p>
 *
 * @author liyong
 * @since 2023-08-31
 */
@Slf4j
@Service
@AllArgsConstructor
public class OrderCustomerRelationshipServiceImpl extends ServiceImpl<OrderCustomerRelationshipMapper, OrderCustomerRelationship> implements OrderCustomerRelationshipService {
    private final OrderCustomerRelationshipMapper orderCustomerRelationshipMapper;

    @Override
    public OrderCustomerRelationship queryByOrderId(String orderId) {
        QueryWrapper<OrderCustomerRelationship> orderCustomerRelationshipQueryWrapper = new QueryWrapper<>();
        orderCustomerRelationshipQueryWrapper.lambda().eq(OrderCustomerRelationship::getOrderId, orderId)
                .eq(BaseEntity::getDeletedFlag, Boolean.FALSE);
        return orderCustomerRelationshipMapper.selectOne(orderCustomerRelationshipQueryWrapper);
    }

    @Override
    public List<Map<String, Object>> orderCustomerRelationships(String customerName) {
        return orderCustomerRelationshipMapper.orderCustomerRelationships(customerName);
    }

    @Override
    public Map<String, Object> orderCustomerRelationship(String customerId) {
        return orderCustomerRelationshipMapper.orderCustomerRelationship(customerId);
    }

    @Override
    public boolean saveWithDesensitized(OrderCustomerRelationship entity) {
        entity.setCustomerMobileMd5sum(MD5.create().digestHex(entity.getCustomerMobile()));
        entity.setCustomerMobile(DesensitizedUtil.mobilePhone(entity.getCustomerMobile()));
        return SqlHelper.retBool(getBaseMapper().insert(entity));
    }
}
